<?php

namespace app\common\model;

use app\common\controller\Helper;
use think\Model;

class OauthIntegral extends Model
{
    use Helper;

    protected $table = 'oauth_integral';

    // 视频学习
    const TYPE_VIDEO = 1;
    // 音频学习
    const TYPE_AUDIO = 2;
    // 背单词
    const TYPE_WORD = 3;
    // 背句子
    const TYPE_SENTENCE = 4;
    // 发帖
    const TYPE_TIE = 5;
    // 评论
    const TYPE_COMMENT = 6;
    // 签到
    const TYPE_SIGN = 7;
    // 购买课程抵扣
    const TYPE_BUY = 8;

    public static function attributes($attribute, $key = null)
    {
        $list = [
            'type' => [
                self::TYPE_VIDEO => '视频学习',
                self::TYPE_AUDIO => '音频学习',
                self::TYPE_WORD => '背单词',
                self::TYPE_SENTENCE => '背句子',
                self::TYPE_TIE => '发帖',
                self::TYPE_COMMENT => '评论帖子',
                self::TYPE_SIGN => '签到',
                self::TYPE_BUY => '购买课程抵扣',
            ],
        ];
        return self::attributeReturn($list, $attribute, $key);
    }

    /**
     * 获取当前用户指定类型当天积分获取总额
     * @param $oauthId
     * @param $type
     * @return int
     */
    public static function dayNum($oauthId, $type)
    {
        $time = strtotime(date('Y-m-d'));
        $model = static::field('SUM((after_num - before_num)) AS num')
            ->where(['oauth_id' => $oauthId, 'type' => $type])
            ->where('create_time', '>=', $time)
            ->select()
            ->toArray();
        return !empty($model[0]['num']) ? $model[0]['num'] : 0;
    }

    /**
     * 获取应该加上的积分数量
     * @param $oauthId
     * @param $type
     * @param array $config 积分配置
     * @return int
     */
    public static function getNum($oauthId, $type, $config = [])
    {
        if (empty($config)) {
            // 查找积分配置
            $config = Config::integralGet($type);
        }

        // 签到
        if ($type == self::TYPE_SIGN) {
            $addNum = $config['integral'];
        } else {
            // 判断是否超出当天最高加的积分
            $addNum = Config::integralMax($oauthId, $type, $config);
        }
        return $addNum;
    }

    /**
     * 添加积分日志
     * @param $oauthId
     * @param int $type 积分类型
     * @param int $addNum 需要加上 或者 扣除的 积分
     * @param int $orderId 购买的班级订单id
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function addOne($oauthId, $type, $addNum, $orderId = 0)
    {
        if ($addNum != 0) {
            // 查找用户
            $oauth = Oauth::getOne($oauthId);

            if (empty($oauth)) {
                return false;
            }

            $beforeNum = $oauth['integral'];
            $afterNum = $beforeNum + $addNum;
            // 购买课程是扣除积分
            $afterNum = $afterNum > 0 ? $afterNum : 0;

            // 添加积分日志
            static::create([
                'oauth_id' => $oauthId,
                'type' => $type,
                'before_num' => $beforeNum,
                'after_num' => $afterNum,
                'order_id' => $orderId,
            ]);

            // 给当前用户累加上积分
            $oauth->integral = $afterNum;
            $oauth->save();
            return true;
        }
        return false;
    }
}